//
//  YTMainViewController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/9.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTMainViewController.h"
#import "YTGiftView.h"
#import "YTNewFeatureView.h"
#import "YTGuildeView.h"
@interface YTMainViewController ()
/**
 子控制器信息数组
 */
@property(nonatomic ,strong)NSArray *controllersInfo;


@end

@implementation YTMainViewController




// Lazy controllersInfo
- (NSArray *)controllersInfo
{
    if (!_controllersInfo) {
        _controllersInfo = @[@{@"clsName":@"YTHomeController",@"title":@"首页",@"tabbarImg":@"icon_shouye"},
                             @{@"clsName":@"YTNewFlashController",@"title":@"快讯",@"tabbarImg":@"icon_kuaixun"},
                             @{@"clsName":@"YTMineController",@"title":@"我的",@"tabbarImg":@"icon_wode"}
                             ];
    }
    return _controllersInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUIWithcontrollersInfoArray:self.controllersInfo];
    
    [self setupNewFeatureView];
    
    self.tabBar.barTintColor = UIColor.whiteColor;
    
}

#pragma mark: - 设置新特性界面
- (void)setupNewFeatureView
{
    UIView *view = [self isNewVerson] ? [YTNewFeatureView newFeaturViewWithFinishedBlock:^{
            
      
        
    }] : nil;
    
    [self.view addSubview:view];
    

}


#pragma mark: - 创建 tabbar 子控制器
- (void)setUpUIWithcontrollersInfoArray:(NSArray *)controllersInfo {

    //创建可变数组
    NSMutableArray *controllers = [NSMutableArray arrayWithCapacity:2];

    //遍历数组创建控制器
    for (NSDictionary *dict in controllersInfo) {
        UIViewController *vc = [self creatChildrenControllerWithDict:dict];

        [controllers addObject:vc];
    }
    [self setViewControllers:controllers.copy];
    
    

}
// MARK: 是否是新版本的判断
- (BOOL)isNewVerson{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //获取当前版本号
    NSString *currentVerson = [NSBundle.mainBundle infoDictionary][@"CFBundleShortVersionString"];
    
    //取出保存在‘用户偏好’的版本号
    NSString *userDefaultVerson = [userDefault objectForKey:@"verson"];
    
    //保存当前版本号到‘用户偏好‘
    [userDefault setObject:currentVerson forKey:@"verson"];
    
    return ![currentVerson isEqualToString:userDefaultVerson];
}

//支持界面直立
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController *)creatChildrenControllerWithDict:(NSDictionary *)dict{

    //获取类名称
    NSString *clsName = dict[@"clsName"];
    NSString *title = dict[@"title"];
    UIImage *image = [UIImage imageNamed:dict[@"tabbarImg"]];
    UIImage *imageHL = [UIImage imageNamed:[NSString stringWithFormat:@"%@_dianji",dict[@"tabbarImg"]]];
    
   
    //创建控制器
    UIViewController *vc = [[NSClassFromString(clsName) alloc] init];

    //创建nav控制器
    YTNavViewController *nav = [[YTNavViewController alloc] initWithRootViewController:vc];
    
    //设置控制器属性
    vc.title = title;
    vc.navigationItem.title = @"以太财经";
    vc.tabBarItem.image =  [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage =  [imageHL imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   
    [self.tabBar setTintColor:kMainColor];
   

    return nav;
}

@end
